package com.sj.manager.controller;

import com.sj.manager.entity.User;
import com.sj.manager.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @desc:
 * @author: xsj
 * @date: 2022/3/17 10:30
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private static final String LOGIN_ERROR ="用户名或密码错码，请重新输入!";
    @Resource
    private UserService userService;
    @RequestMapping("/login")
    public ModelAndView login(User user, HttpSession session) {
        List<User> userList = userService.validateUserInfo(user);
        ModelAndView mv = new ModelAndView();
        if (!CollectionUtils.isEmpty(userList)) {
            mv.addObject("userList",userList);
            mv.setViewName("index");
            //session.setAttribute("userList",userList);
            //return "index";//WEB-INF/views/index.jsp
        } else {
            mv.addObject("error",LOGIN_ERROR);
            mv.setViewName("login");
            //return "login";//WEB-INF/views/login.jsp
        }
        return mv;
    }

}
